#include "asset_store.h"

void assetStore::loadImage(const std::string &path)
{
    SDL_Texture *texture = IMG_LoadTexture(renderer_, path.c_str());
    if(texture == nullptr)
    {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load image: %s", SDL_GetError());
        return;
    }
    Images.emplace(path, texture);
}

void assetStore::loadFont(const std::string &path,int font_size)
{
    TTF_Font *font = TTF_OpenFont(path.c_str(), static_cast<float>(font_size));
    if(font == nullptr){
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load font: %s", SDL_GetError());
        return;
    }
    fonts.emplace(path + std::to_string(font_size), font);
}

void assetStore::loadMusic(const std::string &path)
{
    Mix_Music *music = Mix_LoadMUS(path.c_str());
    if(music == nullptr){
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load music: %s", SDL_GetError());
        return;
    }
    musics.emplace(path, music);
}

void assetStore::loadSound(const std::string &path)
{
    Mix_Chunk *sound = Mix_LoadWAV(path.c_str());
    if(sound == nullptr){
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load sound: %s", SDL_GetError());
        return;
    }
    sounds.emplace(path, sound);
}

SDL_Texture*  assetStore::getImage(const std::string &path)
{
    auto iter = Images.find(path);
    if(iter == Images.end()){
        loadImage(path);
        iter = Images.find(path);
    }
    if(iter == Images.end()){
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load image: %s", path.c_str());
        return nullptr;
    }
    return iter->second;
}

TTF_Font* assetStore::getFont(const std::string &path, int font_size)
{
    //SDL_Log("path:%s, size%d",path.c_str(), font_size);
    auto iter = fonts.find(path + std::to_string(font_size));
    if(iter == fonts.end()){
        loadFont(path, font_size);
        iter = fonts.find(path + std::to_string(font_size));
    }
    if(iter == fonts.end()){
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load font: %s", path.c_str());
        return nullptr;
    }
    return iter->second;
}

Mix_Music* assetStore::getMusic(const std::string &path){
    auto iter = musics.find(path);
    if(iter == musics.end()){
        loadMusic(path);
        iter = musics.find(path);
    }
    if(iter == musics.end()){
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load music: %s", path.c_str());
        return nullptr;
    }
    return iter->second;
}

Mix_Chunk* assetStore::getSound(const std::string &path){
    auto iter = sounds.find(path);
    if(iter == sounds.end()){
        loadSound(path);
        iter = sounds.find(path);
    }
    if(iter == sounds.end()){
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load sound: %s", path.c_str());
        return nullptr;
    }
    return iter->second;
}

void assetStore::clean(){
    for(auto &image : Images){
        SDL_DestroyTexture(image.second);
    }
    for(auto &font : fonts){
        TTF_CloseFont(font.second);
    }
    for(auto &music : musics){
        Mix_FreeMusic(music.second);
    }
    for(auto &sound : sounds){
        Mix_FreeChunk(sound.second);
    }
    Images.clear();
    fonts.clear();
    musics.clear();
    sounds.clear();
}
